package com.mtl.front.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mtl.front.domain.Article;
import com.mtl.front.mapper.TArticleMapper;
import com.mtl.front.service.IArticleService;
import com.mtl.common.tool.model.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 文章管理 服务实现类
 *
 * @author lsc
 * @since 2019-08-02
 */
@Service
public class TArticleServiceImpl implements IArticleService {

    @Autowired
    private TArticleMapper articleMapper;

    @Override
    public Article findById(Integer id) {
        return articleMapper.selectByPrimaryKey(id);
    }

    @Override
    public Page<Article> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<Article> articles=articleMapper.selectAll();
        return new Page<>(articles);
    }




    @Override
    public int saveOrUpdate(Article article) {
        if(article.getId()!=null){
        return articleMapper.updateByPrimaryKey(article);
        }
        return articleMapper.insert(article);
    }

    @Override
    public int deleteBatch(List<Integer> ids) {
        return articleMapper.deleteByIdList(ids);
    }

    @Override
    public List<Article> findAll() {
        return articleMapper.selectAll();
    }

    @Override
    public Page<Article> selectArticlePage(Map<String, Object> map) {
        Integer page= Integer.valueOf ((String) map.get("page"));
        Integer size= Integer.valueOf ((String) map.get("size"));
        PageHelper.startPage(page,size);
        List<Article> list= articleMapper.selectArticlePage(map);
        return new Page<>(list);
    }

    @Override
    public List<Article> orderArticleByPostTime() {
        return articleMapper.orderArticleByPostTime();
    }


}
